package cn.hdu.liang.core.service.product;

import cn.hdu.liang.core.bean.product.Img;
import cn.hdu.liang.core.bean.product.Product;
import cn.hdu.liang.core.bean.product.Sku;
import cn.hdu.liang.core.dao.product.ProductDao;
import cn.hdu.liang.core.query.product.ImgQuery;
import cn.hdu.liang.core.query.product.ProductQuery;
import cn.itcast.common.page.Pagination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商品事务层
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductDao productDao;

    @Resource
    private ImgService imgService;

    @Resource
    private SkuService skuService;

    public Integer addProduct(Product product) {
        //商品编号
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String no = df.format(new Date());
        product.setNo(no);
        product.setCreateTime(new Date());

        //影响行数 商品id

        //保存商品
        Integer i = productDao.addProduct(product);
        //保存图片
        product.getImg().setProductId(product.getId());
        product.getImg().setIsDef(1);
        imgService.addImg(product.getImg());

        //保存sku

        //实例化一个Sku
        Sku sku = new Sku();
        sku.setProductId(product.getId()); //商品ID
        sku.setDeliveFee(10.00);   //运费
        sku.setSkuPrice(0.00);    //售价
        sku.setMarketPrice(0.00);  //市场价
        sku.setStockInventory(0);  // 库存
        sku.setSkuUpperLimit(0);   //购买限制
        sku.setCreateTime(new Date()); //添加时间
        sku.setLastStatus(1);       //是否最新
        sku.setSkuType(1);          //1.商品  0.赠品
        sku.setSales(0);            //销量

        for(String color : product.getColor().split(",")) {
            sku.setColorId(Integer.valueOf(color));
            for(String size : product.getSize().split(",")) {
                //保存sku
                sku.setSize(size);
                skuService.addSku(sku);
            }
        }

        return i;
    }

    @Transactional(readOnly = true)
    public Product getProductByKey(Integer id) {
        Product product = productDao.getProductByKey(id);

        ImgQuery imgQuery = new ImgQuery();
        imgQuery.setProductId(product.getId());
        imgQuery.setIsDef(1);
        List<Img> imgs = imgService.getImgList(imgQuery);
        product.setImg(imgs.get(0));

        return product;
    }

    @Transactional(readOnly = true)
    public List<Product> getProductsByKeys(List<Integer> idList) {
        return productDao.getProductsByKeys(idList);
    }

    public Integer deleteByKey(Integer id) {
        return productDao.deleteByKey(id);
    }

    public Integer deleteByKeys(List<Integer> idList) {
        return productDao.deleteByKeys(idList);
    }

    public Integer updateProductByKey(Product product) {
        return productDao.updateProductByKey(product);
    }

    @Transactional(readOnly = true)
    public Pagination getProductListWithPage(ProductQuery productQuery) {
        Pagination p = new Pagination(productQuery.getPageNo(), productQuery.getPageSize(), productDao.getProductListCount(productQuery));
        List<Product> products = productDao.getProductListWithPage(productQuery);
        //遍历商品添加图片
        for(Product product : products) {
            ImgQuery imgQuery = new ImgQuery();
            imgQuery.setProductId(product.getId());
            imgQuery.setIsDef(1);
            List<Img> imgs = imgService.getImgList(imgQuery);
            product.setImg(imgs.get(0));
        }
        p.setList(products);
        return p;
    }

    @Transactional(readOnly = true)
    public List<Product> getProductList(ProductQuery productQuery) {
        return productDao.getProductList(productQuery);
    }
}
